package ro.gillardon.business.polling;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import ro.gillardon.commons.Price;
import ro.gillardon.commons.config.ConfigParam;
import ro.gillardon.commons.config.ConfigProvider;
import ro.gillardon.commons.enums.Currency;
import ro.gillardon.commons.enums.Hotel;
import ro.gillardon.commons.hotel.HotelDetailsWrapper;
import ro.gillardon.commons.logger.BTPLogger;

/**
 * Parser class for the Commundo hotel from Ismaning.
 *
 * @author lorinczi
 */
public final class CommundoIsmaningParser {

  /**
   * Log4j logging.
   */
  private static final Logger LOG = Logger.getLogger(CommundoIsmaningParser.class);
  private final static String HTTP_HOST = ConfigProvider.getProperty(ConfigParam.HTTP_PROXY_HOST);
  private final static String HTTP_PORT = ConfigProvider.getProperty(ConfigParam.HTTP_PROXY_PORT);
  private final static String HOTEL_URL = ConfigProvider.getProperty(ConfigParam.COMMUNDO_ISMANING_URL);

  /**
   * Private Constructor.
   */
  private CommundoIsmaningParser() {
  }

  private static Document connectAndGetContent() throws IOException {

    System.setProperty("http.proxyHost", HTTP_HOST);
    System.setProperty("http.proxyPort", HTTP_PORT);

    return Jsoup.connect(HOTEL_URL).get();
  }

  /**
   * Parse the document and return the prices in a wrapper.
   *
   * @param document document to parse.
   * @return wrapper with the prices.
   */
  public static HotelDetailsWrapper parseDocumentAndReturnPriceInWrapper(final Document document) {

    Element div = document.getElementById("cbox");
    Elements divs = div.getElementsByClass("bodytext");
    String[] txts = divs.first().text().split(" ");
    HotelDetailsWrapper wrapper = new HotelDetailsWrapper();
    wrapper.setHotel(Hotel.COMMUNDO_ISMANING);

    wrapper.setSingleRoomWeekdayPrice(new Price(Double.parseDouble(txts[13].replaceAll("[^\\d.]", "")), Currency.EUR));
    wrapper.setSingleRoomWeekendPrice(new Price(Double.parseDouble(txts[15].replaceAll("[^\\d.]", "")), Currency.EUR));
    wrapper.setDoubleRoomWeekdayPrice(new Price(Double.parseDouble(txts[29].replaceAll("[^\\d.]", "")), Currency.EUR));
    wrapper.setDoubleRoomWeekendPrice(new Price(Double.parseDouble(txts[31].replaceAll("[^\\d.]", "")), Currency.EUR));
    return wrapper;

  }

  /**
   * getPrice method, gets the price from the webpage and saves it to the database.
   *
   * @return status variable
   */
  public static HotelDetailsWrapper getLatestPrice() {
    try {
      Document document = connectAndGetContent();
      return parseDocumentAndReturnPriceInWrapper(document);
    } catch (IOException e) {
      BTPLogger.error(LOG, "Error connecting to webpage. Details: " + e);
      return null;
    } catch (NumberFormatException e) {
      BTPLogger.error(LOG, "Error parsing input.");
      return null;
    }
  }
}
